package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class NextGreaterNodeInLinkedList {
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public int[] nextLargerNodes(ListNode head) {
            int size = 0;
            ListNode start = head;
            while (head != null) {
                size++;
                head = head.next;
            }
            if (size == 1) {
                return new int[]{0};
            }
            int[] ans = new int[size];
            for (int i = 0; i < size - 1; i++) {
                ListNode index = start.next;
                while (index != null && start.val >= index.val) {
                    index = index.next;
                }
                if (index == null) {
                    ans[i] = 0;
                } else {
                    ans[i] = index.val;
                }
                start = start.next;
            }
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}